package yt.demo.appcoresystem.impl;


import cn.hutool.core.util.ObjectUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import yt.demo.appcommonbean.http.DeleteRequest;
import yt.demo.appcommondb.base.BaseService;
import yt.demo.appcommondb.vo.ConditionVO;
import yt.demo.appcorebasic.annotation.DeleteCache;
import yt.demo.appcoresystem.entity.SystemDictData;
import yt.demo.appcoresystem.entity.SystemDictType;
import yt.demo.appcoresystem.mapper.SystemDictDataMapper;
import yt.demo.appcoresystem.service.ISystemDictDataService;
import yt.demo.appcoresystem.service.ISystemDictTypeService;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class SystemDictDataService extends BaseService<SystemDictData, SystemDictDataMapper> implements ISystemDictDataService {

    ISystemDictTypeService dictTypeService;

    @Autowired
    public SystemDictDataService(SystemDictDataMapper mapper, ISystemDictTypeService dictTypeService) {
        super(mapper, SystemDictData.class);
        this.dictTypeService = dictTypeService;
    }

    @Cacheable(value = "system:dict:data", key = "#params.code")
    @Override
    public List<SystemDictData> getList(SystemDictData params) {
        return getList(params, w -> w.orderByDesc(SystemDictData::getId), whereConditions(params));
    }

    @Override
    @DeleteCache("system:dict:*")
    public SystemDictData cCreate(SystemDictData params) {
        SystemDictType dictType = dictTypeService.detail(params.getTypeId());
        params.setCode(dictType.getCode());
        return create(params);
    }

    @Override
    @DeleteCache("system:dict:*")
    public SystemDictData cUpdate(SystemDictData params) {
        return update(params);
    }

    @Override
    @DeleteCache("system:dict:*")
    public void cDelete(DeleteRequest params) {
        delete(params);
    }

    @Override
    protected List<ConditionVO<SystemDictData, ?>> whereConditions(SystemDictData params) {
        List<ConditionVO<SystemDictData, ?>> conditions = new ArrayList<>();
        if (ObjectUtil.isNotEmpty(params.getTypeId())) {
            conditions.add(ConditionVO.createCondition(SystemDictData::getTypeId, "eq", params.getTypeId()));
        }
        if (ObjectUtil.isNotEmpty(params.getLabel())) {
            conditions.add(ConditionVO.createCondition(SystemDictData::getLabel, "like", params.getLabel()));
        }
        if (ObjectUtil.isNotEmpty(params.getValue())) {
            conditions.add(ConditionVO.createCondition(SystemDictData::getValue, "like", params.getValue()));
        }
        if (ObjectUtil.isNotEmpty(params.getCode())) {
            conditions.add(ConditionVO.createCondition(SystemDictData::getCode, "eq", params.getCode()));
        }
        if (ObjectUtil.isNotEmpty(params.getStatus())) {
            conditions.add(ConditionVO.createCondition(SystemDictData::getStatus, "eq", params.getStatus()));
        }
        return conditions;
    }


}
